package com.lzh.dlykserver.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.lzh.dlykserver.constant.Constants;
import com.lzh.dlykserver.entity.TClueRemark;
import com.lzh.dlykserver.entity.TCustomer;
import com.lzh.dlykserver.entity.TCustomerRemark;
import com.lzh.dlykserver.query.CustomerQuery;
import com.lzh.dlykserver.result.CustomerExcel;
import com.lzh.dlykserver.result.R;
import com.lzh.dlykserver.service.CustomerRemarkService;
import com.lzh.dlykserver.service.CustomerService;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
public class CustomerController {

    @Resource
    private CustomerService customerService;

    @Resource
    private CustomerRemarkService customerRemarkService;

    /**
     * 线索转换为客户
     * @param customerQuery
     * @param token
     * @return
     */
    @PostMapping("/api/clue/customer")
    public R convertCustomer(@RequestBody CustomerQuery customerQuery,
                             @RequestHeader("Authorization") String token) {
        customerQuery.setToken(token);
        Boolean convert = customerService.insertCustomer(customerQuery);
        return convert ? R.OK() : R.FAIL();
    }

    /**
     * 客户分页
     * @param current
     * @return
     */
    @GetMapping("/api/customers")
    public R customerPage(@RequestParam("current") Integer current) {
        if (current == null) {
            current = 1;
        }
        PageInfo<TCustomer> pageInfo = customerService.getCustomerByPage(current);
        return R.OK(pageInfo);
    }

    /**
     * 导出Excel
     * @param response
     * @throws IOException
     */
    @GetMapping("/api/exportExcel")
    public void exportExcel(HttpServletResponse response,
                            @RequestParam(value = "ids", required = false) String ids) throws IOException {

        //要想让浏览器弹出下载框，后端要设置响应头信息
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename="
                + URLEncoder.encode(Constants.EXCEL_FILE_NAME + System.currentTimeMillis(), "utf-8")
                + ".xlsx");

        List<String> idList = StringUtils.hasText(ids) ? new ArrayList<>(Arrays.asList(ids.split(","))) : new ArrayList<>();

        List<CustomerExcel> list = customerService.getCustomerByExcel(idList);

        EasyExcel.write(response.getOutputStream(), CustomerExcel.class)
                .sheet()
                .doWrite(list);
    }

    /**
     * 客户详情
     * @param id
     * @return
     */
    @GetMapping("/api/customer/detail/{id}")
    public R loadCustomer(@PathVariable("id") Integer id) {
        TCustomer tCustomer = customerService.getById(id);
        return R.OK(tCustomer);
    }

    /**
     * 获取客户追踪记录
     * @param current
     * @param customerId
     * @return
     */
    @GetMapping("/api/customer/remark")
    public R customerRemarkPage(@RequestParam(value = "current", required = false) Integer current,
                                @RequestParam("customerId") Integer customerId) {
        CustomerQuery customerQuery = new CustomerQuery();
        customerQuery.setCustomerId(customerId);
        if (ObjectUtils.isEmpty(current)) {
            current = 1;
        }
        PageInfo<TClueRemark> pageInfo = customerRemarkService.getCustomerRemarkByPage(current, customerQuery);
        return R.OK(pageInfo);
    }

    /**
     * 添加客户追踪记录
     * @param customerQuery
     * @param toklen
     * @return
     */
    @PostMapping("/api/customer/remark")
    public R saveCustomerRemark(@RequestBody CustomerQuery customerQuery,
                                @RequestHeader("Authorization") String toklen) {
        customerQuery.setToken(toklen);
        int i = customerRemarkService.saveaClueRemark(customerQuery);
        return i > 0 ? R.OK() : R.FAIL();
    }

    /**
     * 编辑客户追踪记录
     * @param id
     * @return
     */
    @GetMapping("/api/customer/remark/{id}")
    public R getEditedCustomerRemark(@PathVariable("id") Integer id) {
        TCustomerRemark tCustomerRemark = customerRemarkService.getEditedCustomerRemarkById(id);
        return R.OK(tCustomerRemark);
    }

    /**
     * 编辑客户追踪记录
     * @param customerQuery
     * @param toklen
     * @return
     */
    @PutMapping("/api/customer/remark/update")
    public R editCustomerRemark(@RequestBody CustomerQuery customerQuery,
                                @RequestHeader("Authorization") String toklen) {
        customerQuery.setToken(toklen);
        int i = customerRemarkService.editCustomerRemark(customerQuery);
        return i > 0 ? R.OK() : R.FAIL();
    }

    /**
     * 删除客户追踪记录
     * @param id
     * @return
     */
    @DeleteMapping("/api/customer/remark/delete/{id}")
    public R delCustomerMark(@PathVariable("id") Integer id) {
        int i = customerRemarkService.delCustomerMark(id);
        return i > 0 ? R.OK() : R.FAIL();
    }
}
